package com.yunfeisoft.base.dao.impl;

import com.yunfeisoft.base.dao.inter.WorkflowStepUserDao;
import com.yunfeisoft.base.model.User;
import com.yunfeisoft.base.model.WorkflowStep;
import com.yunfeisoft.base.model.WorkflowStepUser;
import com.yunfeisoft.common.base.BaseDaoImpl;
import com.yunfeisoft.common.sql.builder.SelectBuilder;
import com.yunfeisoft.common.sql.builder.WhereBuilder;
import com.yunfeisoft.common.utils.Page;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * ClassName: WorkflowStepUserDaoImpl
 * Description: 用户-步骤Dao实现
 * Author: Jackie liu
 * Date: 2020-01-08
 */
@Repository
public class WorkflowStepUserDaoImpl extends BaseDaoImpl<WorkflowStepUser, String> implements WorkflowStepUserDao {

    @Override
    public Page<WorkflowStepUser> queryPage(Map<String, Object> params) {
        WhereBuilder wb = new WhereBuilder();
        if (params != null) {
            initPageParam(wb, params);
        }
        return queryPage(wb);
    }

    @Override
    public int removeByWorkflowDefineId(String workflowDefineId) {
        String sql = String.format("DELETE FROM %s WHERE WORKFLOW_STEP_ID_ IN (SELECT ID_ FROM TS_WORKFLOW_STEP WHERE WORKFLOW_DEFINE_ID_ = ?)", domainModel.getTableName());
        return jdbcTemplate.update(sql, workflowDefineId);
    }

    @Override
    public List<WorkflowStepUser> queryByWorkflowStepId(String workflowStepId) {
        WhereBuilder wb = new WhereBuilder();
        wb.andEquals("su.workflowStepId", workflowStepId);

        SelectBuilder builder = getSelectBuilder("su");
        builder.column("u.name as userName")
                .column("s.name as stepName")
                .join(User.class).alias("u").on("su.userId = u.id").build()
                .join(WorkflowStep.class).alias("s").on("su.workflowStepId = s.id").build();

        return query(builder.getSql(), wb);
    }
}